package com.coffee.order.service.sku.impl;

import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import com.coffee.order.api.sku.query.NotifyOrderParamQuery;
import com.coffee.order.api.sku.result.NotifyOrderParamResult;
import com.coffee.order.api.sku.service.NotifyOrderParamService;
import com.coffee.order.service.sku.bean.NotifyOrderParam;
import com.coffee.order.service.sku.dao.NotifyOrderParamDao;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class NotifyOrderParamServiceImpl implements NotifyOrderParamService {
	@Resource
	private NotifyOrderParamDao	notifyOrderParamDao;

	@Override
	public Page<NotifyOrderParamResult> getPager(NotifyOrderParamQuery param, PagerInfo pr) {
		Page<NotifyOrderParamResult> pager=new Page<NotifyOrderParamResult>(pr.getStart(), pr.getPageSize());
		Integer count=notifyOrderParamDao.count(param);
		pager.setTotalPage(count);
		if(count>0) {
			List<NotifyOrderParam> list=notifyOrderParamDao.page(param, pr.getStart(), pr.getPageSize());
			pager.setData(DTOUtils.convertList(list, NotifyOrderParamResult.class));
		}
		return pager;
	}
	
	@Override
	public Integer count(NotifyOrderParamQuery param){
		Integer count=notifyOrderParamDao.count(param);
		return count;
	}
	
	@Override
	public NotifyOrderParamResult getDetail(Integer id){
		NotifyOrderParam item=notifyOrderParamDao.findById(id);
		if(item==null){
			return null;
		}
		NotifyOrderParamResult result=new NotifyOrderParamResult();
		BeanUtilExt.copyProperties(result, item);
		
		return result;
	}

	@Override
	public List<NotifyOrderParamResult> getList(NotifyOrderParamQuery param, PagerInfo pr){
		List<NotifyOrderParam> list=notifyOrderParamDao.page(param, pr.getStart(), pr.getPageSize());
		return DTOUtils.convertList(list, NotifyOrderParamResult.class);
	}
	
	@Override
	public List<NotifyOrderParamResult> getList(NotifyOrderParamQuery param){
		List<NotifyOrderParam> list=notifyOrderParamDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, NotifyOrderParamResult.class);
	}
	
	@Override
	public List<NotifyOrderParamResult> getList(){
		NotifyOrderParamQuery param = new NotifyOrderParamQuery();
		List<NotifyOrderParam> list=notifyOrderParamDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, NotifyOrderParamResult.class);
	}


	@Override
	public void delete(Integer id) {
		notifyOrderParamDao.deleteById(id);
	}

	@Override
	public void addOrUpdate(NotifyOrderParamResult result)throws BusinessException {
		NotifyOrderParam item=new NotifyOrderParam();
		BeanUtilExt.copyProperties(item, result);
		if(item.getId()==null) {
			item.setCreateTime(new Date());
			notifyOrderParamDao.insert(item);
		}else {
			NotifyOrderParam tmp=notifyOrderParamDao.findById(item.getId());
			if(tmp==null) {
				throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
			}
			notifyOrderParamDao.updateById(item);
		}
	}
	
	@Override
	public Map<Integer,NotifyOrderParamResult> getItemMap(Set<Integer> idSet){
		Map<Integer, NotifyOrderParamResult> map=new HashMap<Integer, NotifyOrderParamResult>();
		List<NotifyOrderParam> list=notifyOrderParamDao.findByIds(StringUtil.setToList(idSet));
		List<NotifyOrderParamResult> resultList=DTOUtils.convertList(list, NotifyOrderParamResult.class);
		for(NotifyOrderParamResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}
	
	@Override
	public Map<Integer,NotifyOrderParamResult> getItemMap(){
		Map<Integer, NotifyOrderParamResult> map=new HashMap<Integer, NotifyOrderParamResult>();
		NotifyOrderParamQuery param=new NotifyOrderParamQuery();
		List<NotifyOrderParam> list=notifyOrderParamDao.page(param, 0, Integer.MAX_VALUE);
		List<NotifyOrderParamResult> resultList=DTOUtils.convertList(list, NotifyOrderParamResult.class);
		for(NotifyOrderParamResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}	
	
	
	
}
